#!/bin/sh

#Needs diffutils-2.8.1-18 for the diff command.

# Add the following lines to your boot sequence rc.local
# echo " Starting Epaati Update: Please dont power off"
# cd /home/olpc/epaati/
# echo "Unzipping EPaati"
# unzip EPaati.activity.zip
# echo "Copying to Activities Folder"
# cp -fa /home/olpc/epaati/EPaati.activity/* /home/olpc/Activities/EPaati.activity
# cp -fa /home/olpc/version/version /home/olpc/Activities
# echo "Removing temporary Directory"
# rm -fr /home/olpc/epaati
# rm -fr /home/olpc/version

# Script created to update OLE NEPAL Epaati.activity by sulochan acharya --sulochan@olenepal.org
# This script basically gets the source folder from rsync server and puts them into the /home/olpc/#Activities directory. # #The way this is set up is more network intensive than CPU intensive...I did #that becuase the XO has limited space and to #run diff on epaati activity would require copying #huge folders. 

test -d /home/olpc/version && rm -fr /home/olpc/version
test -d /home/olpc/epaati && rm -fr /home/olpc/epaati

mkdir /home/olpc/version
mkdir /home/olpc/epaati
/bin/chown olpc:olpc /home/olpc/verion
/bin/chown olpc:olpc /home/olpc/epaati
# First rsync the content of /version and /remote folders from the rsync server. The version file # contins the
# version number for the epaati activity that will be first compared to the local version file to
# check if the activity is already updated. If the version is the same then compare the file list
# remote.txt to the local file list localq.txt if it matches then the activity is uptodate.
/usr/bin/rsync -avzrpog sugaroffice.ole::ole-update/version/ /home/olpc/version/
/usr/bin/rsync -avzrpog sugaroffice.ole::ole-update/remote/ /home/olpc/version/

# The if statement takes a diff of the local and remote version numbers. If its the same
# it will make a file localq.txt with the file list of the local activity folder.
if
	diff /home/olpc/version/version /home/olpc/Activities/version ; 
	then
		ls -R /home/olpc/Activities/EPaati.activity/ > /home/olpc/version/localq.txt
# If it is not the same then it will rsync the activity to the /tmp/epaati folder, and make
# a file compare.txt with its file list. This will be again compared to the remote.txt to make sure
# that nothing went wrong during the transfer process and is ready to be copied over to the main 
# /home/olpc/Activities folder.		

	else 
		/usr/bin/rsync -avzrpogut sugaroffice.ole::ole-update/ /home/olpc/epaati/
		ls -R /home/olpc/epaati/EPaati.activity > /home/olpc/version/compare.txt
		
	if
		diff /home/olpc/version/remote.txt /home/olpc/version/localq.txt >/dev/null ; then 
		rm -fr /home/olpc/epaati
		rm -fr /home/olpc/version
		rm -fr /home/olpc/EPaati.activity
		exit 
	
	fi
	

fi
# If everything is fine then copy the folder, and remove the files and folder from the
# /temp folder.

# The copying process will be carried out by the bootup script.



